<template>
	<view class="cash">
		<view style="background:#ff580b;background-size: 100%;height: 400upx;">
			<view style="font-size: 16px;color: #FFFFFF;padding-top: 100upx;">设置安全密码</view>
			<!-- <view style="font-size: 29px;color: #FFFFFF;padding-top: 20upx;">¥ {{mayMoney}}</view> -->

			<view style="width: 90%;height: max-content;
			margin-left: 40upx;background-color: #FFFFFF;
			box-shadow: rgba(183, 183, 183, 0.3) 0px 1px 10px;margin-top: 50upx;
			border-radius: 20upx;padding-top: 60upx;padding-bottom: 60upx;">
				<!-- <view style="display: flex;flex-direction: row;padding: 20upx;">
					<view style="font-size: 16px;color: #333333;">提现金额</view>
					<view style="font-size: 11px;color: #333333;margin-left: 20upx;margin-top: 10upx;">{{min}}{{value}}元</view>
				</view> -->
				<!-- <view style="display: flex;flex-direction: row;padding: 20upx;height: 100upx;line-height: 100upx;">
					<view style="font-size: 14px;color: #333333;width: 20%">金额：</view>
					<input type="number" v-model="money" placeholder="请输入金额" style="height: 100upx;line-height: 100upx;font-size: 14px;color: #333333;text-align: left;margin-left: 10upx;width: 80%;" />
				</view> -->
				<view style="display: flex;flex-direction: row;padding: 14upx;height: 100upx;line-height: 100upx;">
					<view style="font-size: 14px;color: #333333;width: 20%">密码：</view>
					<input password="true" v-model="securityCode" placeholder="请输入密码" style="height: 100upx;line-height: 100upx;font-size: 14px;color: #333333;text-align: left;margin-left: 10upx;width: 80%;" />
				</view>
				<view style="display: flex;flex-direction: row;padding: 14upx;height: 100upx;line-height: 100upx;">
					<view style="font-size: 14px;color: #333333;width: 20%">确认：</view>
					<input password="true" v-model="securityCode1" placeholder="请再次输入密码" style="height: 100upx;line-height: 100upx;font-size: 14px;color: #333333;text-align: left;margin-left: 10upx;width: 80%;" />
				</view>
				<view style="display: flex;flex-direction: row;padding: 14upx;height: 100upx;line-height: 100upx;">
					<view style="font-size: 14px;color: #333333;width: 20%;white-space: nowrap;" >手机号：</view>
					<input  v-model="tel" placeholder="请输入手机号码" style="height: 100upx;line-height: 100upx;font-size: 14px;color: #333333;text-align: left;margin-left: 10upx;width: 80%;" />
				</view>
				<view style="display: flex;flex-direction: row;padding: 14upx;height: 100upx;line-height: 100upx;">
					<view style="font-size: 14px;color: #333333;width: 20%;white-space: nowrap;">验证码：</view>
					<input  v-model="verifyCode" placeholder="请输入验证码" style="height: 100upx;line-height: 100upx;font-size: 14px;color: #333333;text-align: left;margin-left: 10upx;width: 60%;" />
					<view class="yzm" @click="yzmf()">{{yzmtext}}</view>
				</view>
				<view style="background: #E6E6E6;width: 100%;height: 1upx;"></view>

				<view style="display: flex;flex-direction: row;flex-wrap: wrap;">
					<view style="display: flex;flex-direction: row;" v-for="(item, index) in moneyList" :key="index">
						<view>
							<view style="padding: 20upx;" @click="getOut1(item.money)">
								<view style="padding-top: 40upx;width: 180upx; height: 120upx;background-color: #FFFFFF;border:1px solid #FF332F;border-radius: 10upx;">
									{{ item.money }}
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view @click="save" style="margin: 60upx;font-size: 18px;background: #e64340;color: white;border-radius: 10px;height: 40px;line-height: 40px">
				保存
			</view>

			<!-- <view style="display: flex;width: 100%;justify-content: center;">
				<view style="color: grey;padding-bottom: 30px;padding-top: 20upx;width: 49%;" @click="goZhifuBao">提现账号</view>
				<view style="color: grey;padding-bottom: 30px;padding-top: 20upx;width: 49%;" @click="list">提现记录</view>
			</view> -->
		</view>
	</view>
</template>

<script>
	import config from '../../../common/config.js'
	export default {
		data() {
			return {
				money: '',
				zhifubao: '',
				mayMoney: '0',
				zhifubaoName: '',
				moneyList: [],
				value: 0,
				min: '',
				from:'',
				userId:'',
				hasBind:false,//是否已经绑定阿里账号
				hasPassword:false, // 是否设置了提现密码
				yzmtext: '验证码',
				tel: '',
				securityCode: '',
				securityCode1: '',
				verifyCode: '',
			};
		},
		onLoad(options) {
			console.log('cash options:',options)
			this.from = options.from
			this.userId = this.$queue.getData('userId');
		},
		onShow: function(e) {
			if(this.from=='mall'){
				this.getMallMoney()
				this.getAliAccount()
				this.checkPassword()
			}else{
				this.getMoney();
				this.getMoneyClassifyList();
				this.getasdas();
			}
			
		},
		methods: {
			getAliAccount(){
				console.log('获取阿里账号的参数：',this.userId)
				uni.request({
					// url:'https://k999az04ootw.ngrok2.xiaomiqiu.cn/p/user/newUserInfo/'+this.userId,
					url:config.mallHost+'/p/user/getBingAliAcc/'+this.userId,
					method:'GET',
					success: (res) => {
						console.log('阿里查询结果：',res)
						if(res.data&&res.data.alipayName){
							this.hasBind = true
						}else{
							this.hasBind = false
						}
					},
					fail: (err) => {
						console.log('阿里查询err',err)
					}
				})
			},
			
			getMallMoney(){
				console.log('userid:'+this.userId)
				console.log('url:'+config.mallHost)
				uni.request({
					// url:'https://k999az04ootw.ngrok2.xiaomiqiu.cn/p/user/newUserInfo/'+this.userId,
					url:config.mallHost+'/p/user/newUserInfo/'+this.userId,
					method:'GET',
					header: {
						"Content-Type": "application/json", //自定义请求头信息
					},
					success: (res) => {
						console.log('个人信息',res)
						// this.mayMoney = res.data.money
						this.mayMoney = res.data.balance
					},
					fail: (err) => {
						console.log('err',err)
					}
				})
			},
			// 查询是否有提现密码
			checkPassword(){
				console.log('userid:'+this.userId)
				uni.request({
					// url:'https://k999az04ootw.ngrok2.xiaomiqiu.cn/p/user/newUserInfo/'+this.userId,
					url:config.mallHost+'/p/user/getSecurityCode/'+this.userId,
					method:'GET',
					header: {
						"Content-Type": "application/json", //自定义请求头信息
					},
					success: (res) => {
						console.log('提现密码：',res)
						if(res.statusCode==200){
							this.hasPassword = true
							// uni.showToast({
							// 	title:'请先设置提现密码',
							// 	icon:'none',
							// 	duration:2000
							// })
						}else{
							this.hasPassword = false
						}
						// this.mayMoney = res.data.money
						// this.mayMoney = res.data.balance
					},
					fail: (err) => {
						console.log('err',err)
					}
				})
			},
			tixian(){
				console.log('userid:'+this.userId)
				if(!this.hasBind){
					uni.showToast({
						title:'请先绑定支付宝账号,进行提现',
						icon:'none',
						duration:2000
					})
					uni.navigateTo({
						url:'/package-task/pages/member/zhifubao?from=mall'
					})
					return
				}
				if(!this.money){
					uni.showToast({
						title:'请输入提现金额',
						icon:'none',
						duration:2000
					})
					return
				}
				if(!this.hasPassword){
					uni.showToast({
						title:'请先设置提现密码',
						icon:'none',
						duration:2000
					})
					return
				}
				if(!this.securityCode){
					uni.showToast({
						title:'请输入提现密码',
						icon:'none',
						duration:2000
					})
					return
				}
				
				let datas = {
					amount:this.money,
					userId:this.userId,
					securityCode:this.securityCode
				}
				console.log('提现参数：',datas)
				uni.request({
					// url:'https://k999az04ootw.ngrok2.xiaomiqiu.cn/p/user/newUserInfo/'+this.userId,
					url:config.mallHost+'/p/order/withdraw',
					method:'POST',
					header: {
						// "Content-Type": "application/json", 
						"Content-Type": "application/x-www-form-urlencoded"
					},
					data:datas,
					success: (res) => {
						console.log('提现结果：',res)
						// this.mayMoney = res.data.money
						if(res.statusCode=200){
							uni.showToast({
								title:'提现成功',
								duration:2000
							})
							this.getMallMoney()
						}
					},
					fail: (err) => {
						console.log('提现err',err)
					}
				})
			},
			// 倒计时
			Downtime(countime) {
			
				this.yzmche = false
				if (countime == 0) {
					// 获取验证码
					this.yzmche = true
					this.yzmtext = '验证码'
					return false
				}
				let _this = this
				setTimeout(function() {
					countime--
					_this.yzmtext = '获取' + countime
					_this.Downtime(countime)
				}, 1000)
			},
			yzmf() {
				// this.yzmche=false;
			
				if (this.yzmche === false) {
					return false
				}
				var reg = /^[1]([3-9])[0-9]{9}$/
				if (!(reg.test(this.tel))) {
			
					uni.showToast({
						title: '请输入正确的手机号',
						icon: 'none',
						duration: 2000
					});
					this.yzmche = true;
					return false
				}
				this.Downtime(60);
				uni.request({
					header: {
						'content-type': 'application/json', //自定义请求头信息
						'userId':this.userId
					},
					url: config.mallHost + '/p/sms/jhSend', //仅为示例，并非真实接口地址。
					method: 'POST',
					data: {
						mobile: this.tel
					},
					success: (res) => {
						console.log('短信请求结果：',res)
						if (res.data.status == 200) {
							uni.showToast({
								title: '发送验证码成功',
								icon: 'success',
								duration: 2000
							});
						}
					},
					fail: (error) => {
						this.yzmche = true
						console.log(error)
					}
				});
			},
			save(){
				if(!this.securityCode){
					uni.showToast({
						title:'请输入密码',
						icon:'none',
						duration:2000
					})
					return
				}
				if(this.securityCode!=this.securityCode1){
					uni.showToast({
						title:'两次密码不一致',
						icon:'none',
						duration:2000
					})
					return
				}
				if(!this.verifyCode){
					uni.showToast({
						title:'请输入验证码',
						icon:'none',
						duration:2000
					})
					return
				}
				
				let datas = {
					userId:this.userId,
					securityCode:this.securityCode,
					code:this.verifyCode
				}
				console.log('提现参数：',datas)
				uni.request({
					// url:'https://k999az04ootw.ngrok2.xiaomiqiu.cn/p/user/newUserInfo/'+this.userId,
					url:config.mallHost+'/p/user/setSecurityCode',
					method:'GET',
					header: {
						// "Content-Type": "application/json", 
						"Content-Type": "application/x-www-form-urlencoded"
					},
					data:datas,
					success: (res) => {
						console.log('密码结果：',res)
						// this.mayMoney = res.data.money
						if(res.statusCode == 400){
							uni.showToast({
								title:res.data,
								icon:'none',
								duration:2000
							})
							return
						}
						if(res.statusCode==200){
							uni.showToast({
								title:'安全密码设置成功',
								duration:2000
							})
							uni.navigateBack({
								delta:1
							})
							uni.$emit('submit')
						}
					},
					fail: (err) => {
						console.log('提现err',err)
					}
				})
			},
			getasdas() {
				this.$Request.getT('/common/type/87').then(res => {
					if (res.code === 0) {
						this.min = res.data.min;
						this.value = res.data.value;
					}
				});
			},
			getMoneyClassifyList() {
				this.$Request.getT('/cashClassify/selectCashClassifyList').then(res => {
					if (res.code === 0) {
						this.moneyList = [];
						res.data.forEach(d => {
							this.moneyList.push(d);
						});
					}
				});
			},
			list() {
				uni.navigateTo({
					url: '/package-task/pages/member/cashList'
				});
			},
			goZhifuBao() {
				uni.navigateTo({
					url: '/package-task/pages/member/zhifubao?from='+this.from
				});
			},
			getMoney() {
				let that = this;
				let token = this.$queue.getData('token');
				let userId = this.$queue.getData('userId');
				if (token) {
					//this.$queue.showLoading("加载中...");
					//可以提现金额查询预估收入查询
					this.$Request.getT('userMoney/selectUserMoney?userId=' + userId).then(res => {
						if (res.code === 0 && res.data) {
							that.mayMoney = res.data.mayMoney;
						} else if (res.code === -102) {
							this.$queue.showToast(res.msg);
							this.$queue.logout();
							uni.navigateTo({
								url: '/package-task/pages/public/login'
							});
						} else {
							that.mayMoney = '0';
							//this.$queue.showToast(res.msg);
						}
					});
					this.$Request.postT('/app/selectUserById?userId=' + userId).then(res => {
						if (res.code === 0 && res.data) {
							that.zhifubao = res.data.zhifubao;
							that.zhifubaoName = res.data.zhifubaoName;
						}
						uni.hideLoading();
					});
				}
			},
			getOut() {
				if('mall'==this.from){
					this.tixian()
					return
				}
				let that = this;
				let token = this.$queue.getData('token');
				let userId = this.$queue.getData('userId');
				if (token) {
					if (that.zhifubao && that.zhifubaoName) {
						if (!/^\d+$/.test(this.money)) {
							uni.showToast({
								icon: 'none',
								title: '请输入正确金额，不能包含中文，英文，特殊字符和小数'
							});
							return;
						}
						if (parseFloat(this.money).toFixed(1) >= this.value) {
							uni.showModal({
								title: '提现申请提示',
								content: '请仔细确认收款人信息\n\n姓名:' + that.zhifubaoName + '\t\t金额:' + this.money + '\n\n收款账号：' + that.zhifubao + '',
								success: e => {
									if (e.confirm) {
										this.$queue.showLoading('提现中...');
										this.$Request.postT('userMoney/cashMoney?userId=' + userId + '&money=' + this.money).then(res => {
											if (res.code === 0) {
												that.$queue.showToast('提现申请成功，预计三个工作日到账');
												that.getMoney();
											} else {
												uni.showModal({
													title: '温馨提示',
													content: res.msg,
													showCancel: false,
													cancelText: '取消',
													confirmText: '确认'
												});
											}
											uni.hideLoading();
										});
									}
								}
							});
						} else {
							this.$queue.showToast('提现金额必须大于或等于10元才可提现');
						}
					} else {
						uni.navigateTo({
							url: '/package-task/pages/member/zhifubao'
						});
					}
				} else {
					uni.navigateTo({
						url: '/package-task/pages/public/login'
					});
				}
			},
			getOut1(money) {
				let that = this;
				let token = this.$queue.getData('token');
				let userId = this.$queue.getData('userId');
				if (token) {
					if (that.zhifubao && that.zhifubaoName) {
						if (parseFloat(money).toFixed(1) >= 10) {
							uni.showModal({
								title: '提现申请提示',
								content: '请仔细确认收款人信息\n\n姓名:' + that.zhifubaoName + '\t\t金额:' + money + '\n\n收款账号：' + that.zhifubao + '',
								success: e => {
									if (e.confirm) {
										this.$queue.showLoading('提现中...');
										this.$Request.postT('userMoney/cashMoney?userId=' + userId + '&money=' + money).then(res => {
											if (res.code === 0) {
												that.$queue.showToast('提现申请成功，预计三个工作日到账');
												that.getMoney();
											} else {
												uni.showModal({
													title: '温馨提示',
													content: res.msg,
													showCancel: false,
													cancelText: '取消',
													confirmText: '确认'
												});
											}
											uni.hideLoading();
										});
									}
								}
							});
						} else {
							this.$queue.showToast('提现金额必须大于或等于10元才可提现');
						}
					} else {
						uni.navigateTo({
							url: '/package-task/pages/member/zhifubao'
						});
					}
				} else {
					uni.navigateTo({
						url: '/package-task/pages/public/login'
					});
				}
			}
		}
	};
</script>

<style lang="less">
	@import '../../static/css/index.css';

	.view2-view-text {
		font-size: 14px;
		color: #000000;
		margin-left: 20upx;
		width: 80%;
	}

	.view2-view-image-right {
		width: 18upx;
		height: 30upx;
		margin-left: 50upx;
	}

	.cash {
		text-align: center;
		background: white;
		height: 100%;
		position: absolute;
		width: 100%;

		.cash-top {
			padding: 32upx 32upx 50upx 32upx;
			/* border-bottom: 1px solid gainsboro; */
			background: #e10a07;
		}

		.leiji {
			font-size: 14px;
			color: #ffffff;
			margin-bottom: 10px;
		}
	}
	.yzm{
		width: 20%;
		background-color: #B4282D;
		border-radius: 6upx;
		height: 80upx;
		line-height: 80upx;
		color: white;
	}
</style>
